#include "CMainWindow.h"
#include "ui_CMainWindow.h"
#include <QDebug>
#include "VSettings.h"
#include "VAbout.h"
#include <QSizePolicy>
#include "CUIHelper.h"
#include "VDialogProtocal.h"
#include "VDialogAscii.h"
#include "CSettings.h"
#include "VDialogPay.h"

CMainWindow::CMainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::CMainWindow)
{
    ui->setupUi(this);
    QIcon iconVer;
    iconVer.addFile(":UI/btn_v_n.png",QSize(32,32),QIcon::Normal);
    iconVer.addFile(":UI/ver_c.png",QSize(32,32),QIcon::Active);
    ui->toolBar->addAction(iconVer,tr("垂直分屏"));
    ui->toolBar->addAction(QIcon(":UI/btn_h_n.png"),tr("水平分屏"));
    ui->toolBar->addAction(QIcon(":UI/btn_close_n.png"),tr("关闭分屏"));
    connect(ui->toolBar,&QToolBar::actionTriggered,[=](QAction *pAction){
//        qDebug()<<pAction->text();
        QString strText = pAction->text();
        if(tr("垂直分屏") == strText){
            ui->vwindowsplitter->slotVSplitScreen();
        }else if(tr("水平分屏") == strText){
            ui->vwindowsplitter->slotHSplitScreen();
        }else if(tr("关闭分屏") == strText){
            ui->vwindowsplitter->slotMergeScreen();
        }
    });
    connect(ui->menu_help,&QMenu::triggered,[=](QAction *pAction){
//        qDebug()<<pAction->text();
        if("action_about" == pAction->objectName()){
            VAbout vabout;
            vabout.exec();
        }else if("action_wave" == pAction->objectName()){
            //非模态形式创建
            VDialogProtocal dialog;
            dialog.exec();
        }else if("action_ascii" == pAction->objectName()){
            VDialogAscii dialog;
            dialog.exec();
        }else if("action_zfb" == pAction->objectName()){
            VDialogPay dialog(VDialogPay::ENM_PAY_ZFB);
            dialog.exec();
        }else if("action_wx" == pAction->objectName()){
            VDialogPay dialog(VDialogPay::ENM_PAY_WX);
            dialog.exec();
        }else if("action_list" == pAction->objectName()){
            VDialogPay dialog(VDialogPay::ENM_LIST);
            dialog.exec();
        }
    });
    connect(ui->menu_settings,&QMenu::triggered,[=](QAction *pAction){
        if("action_preference" == pAction->objectName()){
            VSettings vsettings;
            vsettings.exec();
        }
    });

//    CUIHelper::installQss("D:/TempPro/SuperDTV2/style.qss");


    //初始化状态栏
    initStatusBar();
}

CMainWindow::~CMainWindow()
{
    delete ui;
}

CMainWindow *CMainWindow::getInstance()
{
    static CMainWindow *pInstance = nullptr;
    if(nullptr == pInstance) {
        pInstance = new CMainWindow();
    }
    return pInstance;
}

void CMainWindow::slotSendBytes(int nBytes)
{
     m_pSendLabel->setText(tr("发送:") + QString::number(nBytes) + tr("字节"));
}

void CMainWindow::slotRecvBytes(int nBytes)
{
    m_pRecvLabel->setText(tr("接收:") + QString::number(nBytes) + tr("字节"));
}

void CMainWindow::slotTextCode(QString strTextCode)
{
    m_pTextCodeLabel->setText(strTextCode);
}

void CMainWindow::initStatusBar()
{
    QStatusBar *pBar =  this->statusBar();
    if(nullptr == pBar)
        return ;

    m_pTextCodeLabel = new QLabel();


    m_pSendLabel = new QLabel(pBar);

    m_pRecvLabel = new QLabel(pBar);

    m_pBtnRecount = new QPushButton(pBar);
    m_pBtnRecount->setText(tr("重新计数"));
    connect(m_pBtnRecount,&QPushButton::clicked,[=]{
        emit sigRecount();
    });

    pBar->addPermanentWidget(m_pTextCodeLabel);
    pBar->addPermanentWidget(m_pSendLabel);
    pBar->addPermanentWidget(m_pRecvLabel);
    pBar->addPermanentWidget(m_pBtnRecount);

    slotSendBytes(0);
    slotRecvBytes(0);
    slotTextCode(CSettings::getInstance()->textCode());
}
